package cn.codergege.demo

import cn.codergege.demo.bean.Greeter
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Assertions.assertTrue
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.core.env.Environment
import org.springframework.data.redis.core.RedisTemplate
import org.springframework.data.redis.core.StringRedisTemplate
import org.springframework.test.context.ActiveProfiles
import org.springframework.test.context.junit.jupiter.SpringExtension
import javax.sql.DataSource

@ExtendWith(SpringExtension::class)
@SpringBootTest
@ActiveProfiles("test")
class SpringCacheApplicationTest {

    @Autowired
    lateinit var greeter: Greeter
    @Autowired
    lateinit var dateSource: DataSource
    @Autowired
    lateinit var env: Environment

    @Autowired
    lateinit var stringRedisTemplate: StringRedisTemplate
    @Autowired
    lateinit var redisTemplate: RedisTemplate<Any, Any>
    @Autowired
    lateinit var jsonRedisTemplate: RedisTemplate<Any, Any>

    /**
     *  Redis 常见五大数据类型:
     *      1) String,
     *      2) List,
     *      3) Set,
     *      4) Hash(散列),
     *      5) ZSet(有序集合)
     *  RedisTemplate 默认使用 jdk 序列化规则
     *  可以用 json 序列化规则
     */
    @Test
    fun testRedis() {
        // String
        stringRedisTemplate.opsForValue().append("msg", "hello")
        val msg = stringRedisTemplate.opsForValue().get("msg")
        println(msg)
        //List
        stringRedisTemplate.opsForList().leftPush("mylist", "1")
        stringRedisTemplate.opsForList().leftPush("mylist", "2")
        stringRedisTemplate.opsForList().leftPushAll("mylist", "3", "4")
        val mylistSize = stringRedisTemplate.opsForList().size("mylist")
        println(mylistSize)

        // 保存对象
        redisTemplate.opsForValue().set("greeter", greeter)
        val g = redisTemplate.opsForValue().get("greeter")
        println(g)

        // json 序列化规则
        jsonRedisTemplate.opsForValue().set("greeter", greeter)
        val g2 = jsonRedisTemplate.opsForValue().get("greeter")
        println("g2: $g2")
    }

    @Test
    fun testApp() {
        println("测试...")
        assertTrue(true)
        println(dateSource::class.java)
        println(env.activeProfiles)
    }

    @Test
    fun testGreeter() {
        assertEquals("This is test environment", greeter.greetingMsg)
    }
}
